package com.data;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

import com.bean.ExamineBean;
import com.constant.Constant;
import com.net.ClientError;
import com.net.ClientException;
import com.net.RequestParameter;

/**
 * 获取学生考试信息
 * 需要参数为
 * <url></url> : 请求连接
 * <referer></referer> ： 转发url
 */
public final class ExamineData extends BaseData
{

	private final static String TAG = "examine_data";
	
	private ArrayList<ExamineBean> mExamineList;
	
	@Override
	public void startParse(RequestParameter parameter) throws Exception, ClientException
	{
		Log.i(TAG, "开始获取考试查询信息");
		
		String url = parameter.getValue("url");
		if(url == null)
			throw new NullPointerException("请求连接url为null");
		
		String referer = parameter.getValue("referer");
		if(referer == null)
			throw new NullPointerException("请求转发referer为null");
		
		final String name = parameter.getValue("name");
		
		String stream = openConnection(url, referer, name);
		if(stream == null)
			return;
		readStream(stream);
		
	} // startParse
	
	
	private String openConnection(final String urls, final String referer, final String name) throws Exception, ClientException
	{
		
		URL url = new URL(urls.replaceAll(name, URLEncoder.encode(name, "utf-8")));
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(Constant.SET_CONNECTION_TIMEOUT);
		connection.setReadTimeout(Constant.SET_READ_TIMEOUT);
		connection.setUseCaches(false);
		connection.setRequestMethod("GET");
		connection.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*");
		connection.setRequestProperty("Referer", referer);
		connection.setRequestProperty("Host", "jwc.xhu.edu.cn");
		connection.connect();
		
		if(connection.getResponseCode() != 200)
		{
			Log.i(TAG, "网络请求错误, 错误代码：" + connection.getResponseCode());
			throw new ClientException(connection.getResponseCode() + "", ClientError.ERROR_CONNECTION);
		}
		
		return getWebSource(connection.getInputStream(), "gb2312");
		
	} // openConnection
	
	/**
	 * 数据格式如下：
		<table class="datelist" cellspacing="0" cellpadding="3" border="0" id="DataGrid1" width="100%"> 
		 <tbody>
		  <tr class="datelisthead"> 
		   <td>选课课号</td>
		   <td>课程名称</td>
		   <td>姓名</td>
		   <td>考试时间</td>
		   <td>考试地点</td>
		   <td>考试形式</td>
		   <td>座位号</td>
		   <td>校区</td> 
		  </tr>
		  <tr> 
		   <td>(2012-2013-1)-0303411-0120040034-1</td>
		   <td>形势与政策</td>
		   <td>莫建华</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td> 
		  </tr>
		  <tr class="alt"> 
		   <td>(2012-2013-1)-6015089-0119980052-1</td>
		   <td>应用统计</td>
		   <td>莫建华</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td>
		   <td>&nbsp;</td> 
		  </tr> 
		 </tbody>
		</table>
	 * @param stream
	 */
	private void readStream(final String stream)
	{
		Log.i(TAG, "开始解析html数据");
		
		Document document = Jsoup.parse(stream);
		Elements tables = document.getElementsByClass("datelist");
//		System.out.println(tables.html().toString());

		if ( tables == null )
			return;
		
		
		Elements trs = tables.select("tr");
		if ( trs == null )
			return;
		
		
		mExamineList = new ArrayList<ExamineBean>();
		// 跳过标题
		for ( int i = 1, len = trs.size(); i < len; i++ )
		{
			Element td 			= 	trs.get(i);
			ExamineBean bean 	=	new ExamineBean();
			bean.examineId 		= 	td.child(0).ownText();
			bean.courseName 	= 	td.child(1).ownText();
			bean.name			=	td.child(2).ownText();
			bean.time			=	td.child(3).ownText();
			bean.site			=	td.child(4).ownText();
			bean.examineFormat	=	td.child(5).ownText();
			bean.seatNumber		=	td.child(6).ownText();
			bean.campus			=	td.child(7).ownText();
			
			mExamineList.add(bean);
		}
	
		Log.i(TAG, "考试信息解析成功");
		
	} // readStream
	
	public ArrayList<ExamineBean> getExamineList()
	{
		return this.mExamineList;
	}
	
	public void printData()
	{
		for ( int i = 0; i < mExamineList.size(); i++ )
		{
			ExamineBean bean = mExamineList.get(i);
			System.out.println(bean.examineId);
			System.out.println(bean.courseName);
			System.out.println(bean.name);
			System.out.println(bean.time);
			System.out.println(bean.site);
			System.out.println(bean.examineFormat);
			System.out.println(bean.seatNumber);
			System.out.println(bean.campus);
		}
	}
}
